<template>
  <div class="bigbox">
    <div class="box">
      <el-card class="card">
        <template
          ><img src="http://weldon.net.cn/back/img/logo2.fc048282.png" alt=""
        /></template>
        <el-form :model="form" :rules="rules" ref="form">
          <el-form-item prop="username">
            <el-input
              placeholder="用户名"
              prefix-icon="el-icon-user-solid"
              v-model="form.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              placeholder="密码"
              prefix-icon="el-icon-unlock"
              v-model="form.password"
            ></el-input>
          </el-form-item>
          <el-form-item class="buttom">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { login } from "../../api/user";
import { Message } from "element-ui";
export default {
  name: "LoginIndex",
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: ["blur", "change"],
          },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: ["blur", "change"],
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: ["blur", "change"],
          },
          {
            pattern: /^\w{6,15}$/,
            message: "请输入 6 到 15 位的密码",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    async login() {
      try {
        const loging = await this.$refs.form.validate();
        // console.log(loging);
        if (loging) {
          const res = await login(this.form);
          this.$store.commit("user/setUserToken", res.data.token);
          this.$router.push("/");
          Message.success("登录成功");
          console.log(res);
        }
      } catch (e) {
        console.log(e);
      }
    },
    reset() {
      this.$refs.form.resetFields();
    },
  },
};
</script>

<style scoped>
.bigbox {
  margin: 0;
  padding: 0;
  background-color: #2b4b6b;
  min-height: 760px;
}
.box {
  padding-top: 200px;
}
.card {
  width: 420px;
  margin: 0 auto;
  text-align: center;
  color: white;
}
.buttom {
  float: right;
}
</style>
